/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.timed;

import com.sun.faces.application.resource.ResourceManager;
import entity.Construct;
import entity.Player;
import entity.Storage;
import entity.StoragePK;
import java.util.Collection;
import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import session.BattleBean;
import session.manager.MessageManagerBeanLocal;
import session.manager.PlayerManagerBeanLocal;
import session.manager.ResourceManagerBeanLocal;
import util.Wrapper;

/**
 *
 * @author antonio
 */
@Stateless
public class BattleTimedBean implements BattleTimedBeanLocal {

    @Resource
    private TimerService timerService;
    @EJB
    private PlayerManagerBeanLocal playerManager;
    @EJB
    private ResourceManagerBeanLocal resourceManager;
    @EJB
    private MessageManagerBeanLocal messageManager;

    @Override
    public void initTimer(String username1, int amount, String username2) {
        Wrapper wrapper = new Wrapper(username1, username2, amount);
        TimerConfig timerConfig = new TimerConfig(wrapper, true);
        Timer timer = timerService.createSingleActionTimer(60 * 1000, timerConfig);
        System.out.print("TimedBean: start");
    }

    @Timeout
    public void timeout(Timer timer) {
        Wrapper wrapper = (Wrapper) timer.getInfo();
        Player player1 = playerManager.find(wrapper.getString1());
        if (player1 == null) {
            return;
        }
        Player player2 = playerManager.find(wrapper.getString2());
        if (player2 == null) {
            resourceManager.increse(wrapper.getString1(), BattleBean.RESOURCE, wrapper.getAmount());
        }
        double r1 = Math.random() * wrapper.getAmount();
        double r2 = Math.random() * resourceManager.getResourceAmount(wrapper.getString2(), BattleBean.RESOURCE);
        if (r1 > r2) {
            Collection<Storage> storages1 = player1.getStorageCollection();
            for (Storage s : storages1) {
                if (s.getResource1().getName().equals(BattleBean.RESOURCE)) {
                    resourceManager.increse(player1.getUsername(), s.getResource1().getName(), (int) (wrapper.getAmount() * 0.75));
                } else {
                    resourceManager.increse(player1.getUsername(), s.getResource1().getName(), (int) (s.getAmount() * 0.25));
                }
            }
            Collection<Storage> storages2 = player2.getStorageCollection();
            for (Storage s : storages2) {
                resourceManager.decrese(player2.getUsername(), s.getResource1().getName(), (int) (s.getAmount() * 0.25));
            }
            messageManager.createMessage(player1.getUsername(), "Hai vinto l'assedio ! :D... il tuo punteggio: " + r1 + ", il punteggio del tuo avversario è: " + r2);
            messageManager.createMessage(player2.getUsername(), "Sei stato assediato ! :(... il tuo punteggio: " + r2 + ", il punteggio del tuo avversario è: " + r1);
        } else {
            resourceManager.increse(player1.getUsername(), BattleBean.RESOURCE, (int) (wrapper.getAmount() * 0.75));
            int amount2 = resourceManager.getResourceAmount(player2.getUsername(), BattleBean.RESOURCE);
            resourceManager.decrese(player2.getUsername(), BattleBean.RESOURCE, (int) (amount2 * 0.25));
            messageManager.createMessage(player1.getUsername(), "Le tue truppe si ritirano ! :(... il tuo punteggio: " + r1 + ", il punteggio del tuo avversario è: " + r2);
            messageManager.createMessage(player2.getUsername(), "Sei riuscito a scacciare gli invasori ! :D... il tuo punteggio: " + r2 + ", il punteggio del tuo avversario è: " + r1);
        }
        System.out.print("TimedBean: end");
    }
}
